//
//  SDSessionManager.m
//  skinDetection
//
//  Created by  liangzhun on 2018/11/7.
//  Copyright © 2018年  liangzhun. All rights reserved.
//

#import "SDSessionManager.h"

@implementation SDSessionManager
+ (instancetype)shareManager
{
    static SDSessionManager *manager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        manager = [[SDSessionManager alloc] initWithBaseURL:[NSURL URLWithString:BASE_URL]];
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/xml", @"text/plain", nil];
        manager.requestSerializer = [AFJSONRequestSerializer serializer];
        manager.requestSerializer.timeoutInterval = 10.0;
        [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
        
    });
    
    if (SDAppDelegate.userModel.token.length > 0) {
        [manager.requestSerializer setValue:SDAppDelegate.userModel.token forHTTPHeaderField:@"Token"];
    }
    
    return manager;
}

+ (void)SDPOST:(NSString *)URLString parameters:(id __nullable)parameters success:(void (^)(NSURLSessionDataTask * _Nonnull task, NSDictionary *_Nullable responseObject))success failure:(void (^)(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error))failure
{
    [[self shareManager] POST:URLString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        success(task ,responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (error.code == -1001){
            [SDMethodTool showWarnWithContent:@"网络请求超时"];
        }
        failure(task ,error);
    }];
    
}

+ (void)SDGET:(NSString *)URLString parameters:(id __nullable)parameters success:(void (^)(NSURLSessionDataTask * _Nonnull task, NSDictionary *_Nullable responseObject))success failure:(void (^)(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error))failure
{

    [[self shareManager] GET:URLString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
         success(task ,responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        if (error.code == -1001){
            [SDMethodTool showWarnWithContent:@"网络请求超时"];
        }
        failure(task ,error);
    }];
    
}
+ (void)SDPOST:(NSString *)URLString
                     parameters:(id)parameters
      constructingBodyWithBlock:(void (^)(id <AFMultipartFormData> formData))block
                       progress:(nullable void (^)(NSProgress * _Nonnull))uploadProgress
                        success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
                        failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure

{
    [[self shareManager] POST:URLString parameters:parameters constructingBodyWithBlock:block progress:uploadProgress success:success failure:failure];
}
@end
